package com.letwal.cmcvnd.mywallet

import android.annotation.SuppressLint
import android.view.View
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.letwal.cmcvnd.base.BaseActivity
import com.letwal.cmcvnd.aviewmodel.CoinsViewModel
import com.letwal.cmcvnd.data.ReqBase
import com.letwal.cmcvnd.databinding.ActivityMyWalletBinding
import com.letwal.cmcvnd.enums.PageTagEnum
import com.letwal.cmcvnd.extend.*
import com.letwal.cmcvnd.mywallet.adapter.MyWalletAdapter
import com.letwal.cmcvnd.home.repo.HomeCoinManager

/** 我的币页 */
@SuppressLint("CustomSplashScreen")
class MyWalletActivity : BaseActivity() {

    //布局binding
    private lateinit var mActivityBinding: ActivityMyWalletBinding

    //适配器
    private lateinit var mMyWalletAdapter: MyWalletAdapter

    //coins viewModel
    private val mCoinsViewModel by lazy {
        ViewModelProvider(this)[CoinsViewModel::class.java]
    }

    override fun onBindingViewModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mCoinsViewModel)
        return list
    }

    override fun onBindingViewTag(): String {
        return PageTagEnum.my_wallet.name.uppercase()
    }

    override fun onBindingContentView(): View {
        mActivityBinding =
            ActivityMyWalletBinding.inflate(layoutInflater)
        return mActivityBinding.root
    }

    override fun onContentViewBinded() {
        try {
            //初始化刷新组件
            initRefreshUI()

            //监听返回
            onHandleLiveDataCallback()

            //初始化适配器
            mMyWalletAdapter = MyWalletAdapter(HomeCoinManager.mCoins)
            mMyWalletAdapter.setItemClickListener { action, item ->
                if (action == MyWalletAdapter.ACTION_CLICK) {
                    //保存全局信息
                    HomeCoinManager.updateGlobalCoinType(item.coinType)
                    HomeCoinManager.updateGlobalMainCoinType(item.mainCoinType)
                    HomeCoinManager.updateGlobalName(item.name)
                    HomeCoinManager.updateGlobalSymbol(item.symbol)
                    HomeCoinManager.updateGlobalAddress(item.address)

                    //退出
                    finish()

                } else if (action == MyWalletAdapter.ACTION_MORE) {
                    //组装参数
                    val coinType = item.coinType ?: ""
                    val params = HashMap<String, String>()
                    params["coinType"] = coinType
                    startToManageWalletActivity(params)
                }
            }
            mActivityBinding.listMyWallet.layoutManager = LinearLayoutManager(this)
            mActivityBinding.listMyWallet.adapter = mMyWalletAdapter

            //点击确定
            mActivityBinding.llMyWalletAdd.setOnClickListener {
                startToAddWalletActivity()
            }

            //获取coins列表
            mCoinsViewModel.getCoins(ReqBase(showLoading = false))

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }


    /** 监听live data 返回 */
    @SuppressLint("NotifyDataSetChanged")
    private fun onHandleLiveDataCallback() {
        mCoinsViewModel.onGetCoinsLiveData.observe(this) {
            stopRefreshState()
            if (it.isSuccess) {
                it.getOrNull()?.let { result ->
                    HomeCoinManager.mCoins.clear()
                    HomeCoinManager.mCoins.addAll(result)
                    mMyWalletAdapter.notifyDataSetChanged()

                    if (HomeCoinManager.mCoins.size > 0) {
                        mActivityBinding.emptyLayout.llEmptyList.visibility = View.GONE
                    } else {
                        mActivityBinding.emptyLayout.llEmptyList.visibility = View.VISIBLE
                    }
                }
            }
        }
    }

    /** 初始化下拉刷新 */
    private fun initRefreshUI() {
        connectSwipeRefreshLayout(mActivityBinding.swipeRefresh) {
            //获取coins列表
            mCoinsViewModel.getCoins(ReqBase(showLoading = false))
        }
    }
}